package futbolfacil.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import junit.framework.Assert;

import futbolfacil.excepcion.DAOExcepcion;
import futbolfacil.modelo.Dueno;
import futbolfacil.modelo.Local;
import futbolfacil.negocio.SeguridadNegocio;

/**
 * Servlet implementation class LocalServlet
 */
@WebServlet("/LocalServlet")
public class LocalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public LocalServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		SeguridadNegocio neg = new SeguridadNegocio();
		Collection<Dueno> listDuenos = new ArrayList<Dueno>();
		try {
			listDuenos = neg.listarDuenos();
		} catch (Exception e) {
			request.setAttribute("msg1", e.getMessage());
			request.setAttribute("valor", "true");
		}
		
		Collection<Local> listLocal = new ArrayList<Local>();
		try {
			listLocal = neg.ListarLocal();
		} catch (Exception e) {
			request.setAttribute("msg1", e.getMessage());
			request.setAttribute("valor", "true");
		}
		
		request.setAttribute("ListadoLocal", listLocal);
		request.setAttribute("ListadoDuenos", listDuenos);
		RequestDispatcher rd = request.getRequestDispatcher("/frmLocal.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		Local oLocal = new Local();
		Dueno oDueno = new Dueno();
		SeguridadNegocio neg = new SeguridadNegocio();

		try	{
			int codDueno = Integer.parseInt(request.getParameter("cboDueno"));
			oDueno = neg.buscarDuenoPorCodigo(codDueno);
		} catch (DAOExcepcion e) {
			request.setAttribute("msg1", e.getMessage());
			request.setAttribute("valor", "true");
		}

		oLocal.setDuenoLocal(oDueno);
		oLocal.setDescripcionLocal(request.getParameter("txtNombre"));
		oLocal.setDireccionLocal(request.getParameter("txtDireccion"));
		oLocal.setDistritoLocal(request.getParameter("cboDistrito"));
		oLocal.setGooMapLocal(request.getParameter("txtMapa"));
		oLocal.setTelefonoLocal(request.getParameter("txtTelefono"));
		
		try {
			neg.InsertarLocal(oLocal);
			response.sendRedirect(request.getContextPath() + "/LocalServlet");
		} catch (DAOExcepcion e){
			request.setAttribute("msg1", e.getMessage());
			request.setAttribute("valor", "true");
		}
		
	}

}
